﻿
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>


/*
 * ./zmq_sr04_test /dev/zmq_sr04
 *
 */

int main(int argc, char **argv)
{

	int fd, ret;
	unsigned int time_ns ;
	unsigned int distance;

	if (argc != 2)
	{
		printf("Usage: %s <dev>\n", argv[0]);
		return -1;
	}
	
	fd = open(argv[1], O_RDWR);
	if (fd == -1)
	{
		printf("Open %s err.\n", argv[1]);
		return -1;
	}
	
	while(1)
	{
			
		if (read(fd, &time_ns, 4) == 4)
		{
			distance = time_ns * 340 / 2 / 1000000;
			printf("get time: %u ns\n", time_ns);
			printf("get distance: %u mm\n", distance);

		}
		else
			printf("get NAN: -1\n");
		sleep(2);
	}

	close(fd);
	return 0;
}


